<template>
  <!-- 广告幻灯片 -->
  <div class="advert">
    <tableComponents :isHaveExport="false" :isHavePagination="false" :isShow="false" @toAdd="editAdvert">
      <template v-slot:table>
        <el-table ref="multipleTable" :data="list" v-loading="loading" stripe header-row-class-name="head_color"
          tooltip-effect="dark" style="width: 99.9%" :default-sort="{ prop: 'sort' }" @selection-change="handleSelectionChange">
          <el-table-column type="selection" class="tc" width="35"></el-table-column>
          <el-table-column prop="sort" sortable :sort-method="sorta" label="排序" min-width="80" width="80">
            <template slot-scope="scope">
              <el-input @change="sortinfo(scope.$index, scope.row)" v-model.number="scope.row.sort" autocomplete="off"
                size="small"></el-input>
            </template>
          </el-table-column>
          
          <el-table-column prop="picture" header-align="center" align="center" width="220px" label="图片">
            <template slot-scope="scope">
              <el-popover placement="right" title trigger="hover">
                <img :src="scope.row.piclink" style="width:500px;" />
                <img slot="reference" :src="scope.row.piclink" :alt="scope.row.piclink" style="width:100px" />
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column label="标题" min-width="100">
            <template slot-scope="scope">
              <div>
                <span>标题：</span>
                <small>{{ scope.row.title }}</small>
                <br />
                <span>英文标识：</span>
                <small>{{ scope.row.iden }}</small>
                <br />
              </div>
            </template>
          </el-table-column>

          <el-table-column label="描述" min-width="100">
            <template slot-scope="scope">{{ scope.row.description }}</template>
          </el-table-column>

          <el-table-column prop="name" label="是否开启" min-width="80">
            <template slot-scope="scope">
              <el-switch class="zdySwith" :width="82" v-model="scope.row.show" active-color="#1aad19" inactive-color="#ffffff"
                active-text="已开启" inactive-text="已关闭" active-value="1" @change="getIs($event, scope.row)"
                inactive-value="0"></el-switch>
            </template>
          </el-table-column>

          <el-table-column label="操作" min-width="120">
            <template slot-scope="scope">
              <el-button :loading="$store.state.isLoading" type="primary" size="mini" @click="editAdvert(scope.row)">编辑</el-button>
              <el-button :loading="$store.state.isLoading" size="mini" type="danger" @click="deleteNew(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-slot:footerLeft>
        <a class="aBtn aBtn_1 aBtn_lan2" @click="deleteLises">删除</a>
      </template>
    </tableComponents>

    <el-dialog title="编辑广告幻灯片" :visible.sync="info_show" class="tl size_13" width="800px" :before-close="handleClose" :close-on-click-modal="false"
      :modal-append-to-body="false">
      <el-form :model="info" ref="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item label="标题" prop="title">
          <el-col :span="12">
            <el-input type="text" v-model="info.title"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="英文标识" prop="iden">
          <el-col :span="12">
            <el-input type="text" v-model="info.iden"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="图片" prop="piclink">
          <el-col :span="12">
            <el-input v-model="info.piclink" disabled></el-input>
            <uploadpic :piclink="info.piclink" :width="300" :height="175" :real_width="140" :real_height="90" :is_more="false"
              @send_pic="onSuccess"></uploadpic>
          </el-col>
        </el-form-item>
        <el-form-item label="链接" prop="links">
          <el-col :span="12">
            <el-input type="text" v-model="info.links"></el-input>
          </el-col>
          <el-col :span="4" style="margin-left:15px">
            <LinkMenu @sendRow="getSendRow"></LinkMenu>
          </el-col>
        </el-form-item>
        <el-form-item label="描述">
          <el-col :span="12">
            <el-input type="text" v-model="info.description"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label-width="120px">
          <el-button type="primary" @click="saveInfo('ruleForm')">编辑</el-button>
          <el-button @click="info_show = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  import uploadpic from "@/components/page/pic/up_pic";
  import LinkMenu from "@/components/currency/Link_menu.vue";
  export default {
    data() {
      return {
        loading: false,
        list: [],
        multipleSelection: [],
        sort: "",
        searchAll: {},
        info: {},
        open_time: "",
        info_show: false,
        rules: {
          title: [{
            required: true,
            message: "请输入标题",
            trigger: "blur"
          }],
          iden: [{
            required: true,
            message: "请输入标识",
            trigger: "blur"
          }],
          piclink: [{
            required: true,
            message: "请选择图片",
            trigger: "blur"
          }]
        },
      };
    },
    components: {
      uploadpic,
      LinkMenu
    },
    methods: {
      onSuccess(pic) {
        // 图片
        this.info.piclink = pic;
      },
      getSendRow(row) {
        // 链接
        this.$store.dispatch("getShopsAll", {
          page: 1,
          page_size: 10
        });
        var result = this.$fnc.getSendRow(row);
        this.info.links = result.links;
      },
      editAdvert(row) {
        // 编辑修改信息
        if (row) {
          this.info = row;
          this.open_time = this.$fnc.vuetime(row.open_time);
        } else {
          this.info = {
            iden: "",
            title: "",
            piclink: "",
            links: "",
            description: ""
          }
        }
        this.info_show = true;
      },
      saveInfo(formName) {
        // 保存信息编辑
        this.$refs[formName].validate(valid => {
          if (valid) {
            var params = this.info;
            if (params.id) {
              // 编辑
              this.$api.getAdvert.editAdvert(params).then(res => {
                if (res.data.code == 200) {
                  this.$message.success("修改成功");
                  this.getInfoAll();
                  this.info_show = false;
                } else {
                  this.$message.error(res.data.result);
                  this.info_show = false;
                }
              })
            } else {
              // 添加
              params.cate = this.$route.query.action;
              this.$api.getAdvert.addAdvert(params).then(res => {
                if (res.data.code == 200) {
                  this.$message.success("添加成功");
                  this.getInfoAll();
                  this.info_show = false;
                } else {
                  this.$message.error(res.data.result);
                  this.info_show = false;
                }
              })
            }
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      sortinfo(index, row) {
        // 排序
        this.info = row;
        this.saveInfo();
      },
      getIs(index, row) {
        // 是否开启
        this.info = row;
        this.saveInfo();
      },
      getInfoAll(obj) {
        // 查询
        this.loading = true;
        var _this = this;
        var params = obj || {};
        params.cate = this.$route.query.action || "";
        this.$api.getAdvert.getAdvertList(params).then(res => {
          if (res.data.code == 200) {
            _this.list = res.data.result;
          }
          _this.loading = false;
        })
      },
      handleClose() {
        // 关闭弹框
        this.info_show = false;
      },
      deleteNew(id) {
        // 删除数据
        this.$swal({
          title: "您确定要删除这条信息吗",
          text: "删除后将无法恢复，请谨慎操作！",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "是的，我要删除",
          cancelButtonText: "容我三思"
        }).then(willDelete => {
          if (willDelete.value) {
            var data = {
              id_str: id
            };
            this.$api.getAdvert.delAdvert(data).then(res => {
              if (res.data.code == 200) {
                this.$swal("删除成功！", "您已经删除了这条信息。", "success");
                this.getInfoAll();
              } else {
                this.$swal("删除失败！", "出错啦！", "success");
              }
            });
          }
        });
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      deleteLises() {
        //批量删除
        var str = "";
        if (this.multipleSelection.length <= 0) {
          this.$message.error("未选择批量删除");
        } else {
          for (var i in this.multipleSelection) {
            str += this.multipleSelection[i].id + "@";
          }
          str = str.slice(0, -1);
          this.$swal({
            title: "您确定要删除这些信息吗",
            text: "删除后将无法恢复，请谨慎操作！",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的，我要删除",
            cancelButtonText: "容我三思"
          }).then(willDelete => {
            if (willDelete.value) {
              var data = {
                id_str: str
              };
              this.$api.getAdvert.delAdvert(data).then(res => {
                if (res.data.code == 200) {
                  this.$swal("删除成功！", "您已经删除了这些信息。", "success");
                  this.getInfoAll();
                } else {
                  this.$swal("删除失败！", "出错啦！", "success");
                }
              });
            }
          });
        }
      },
      sorta(a, b) {
        return b - a;
      }
    },
    created() {
      this.getInfoAll();
    },
    computed: {},
    watch: {}
  };
</script>

<style>
</style>
